package sany.iec.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sany.iec.common.GlobalConstant;
import sany.iec.service.olp01.Olp01ServerServiceImpl;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author : 托马斯
 * @Date : 2025/7/2 19:48
 * @Description :
 */
@RestController
@RequestMapping("/olp01")
@Slf4j
public class OLP01Controller {

    @Resource
    private Olp01ServerServiceImpl olpServerService;


    @GetMapping("/startorstop")
    public Object startOrStopEms(@RequestParam("flag") Integer flag) throws IOException {
        if (flag == null || flag == 0) {
            if (!olpServerService.getServer().isStopped()) {
                olpServerService.stopEmsServer();
            }
            return "停止olp服务器";
        } else if (flag == 1) {
            if (olpServerService.getServer().isStopped()) {
                olpServerService.startEmsServer();
            }
            return "启动olp服务器";
        }
        return "只能输入0或者1,0停止，1启动";
    }

    @GetMapping("/update")
    public Object updateValue(@RequestParam("rate") Float rate, @RequestParam("voltage") Float voltage) throws IOException {
        if (-10 <= rate && rate <= -5) {
            GlobalConstant.RATE = rate;
        }else {
            return "斜率设置错误，[-10, -5]";
        }
        if (105 <= voltage && voltage <= 125) {
            GlobalConstant.RATED_VOLTAGE = voltage;
        }else {
            return "电压设置错误, [105, 125]";
        }
        return "成功，" + rate + " " + voltage;
    }

}
